package de.revdev.llt.bean.managed;

import de.revdev.llt.bean.entities.Category;
import de.revdev.llt.bean.entities.FileCardBox;
import de.revdev.llt.bean.entities.PublicClassRoom;
import de.revdev.llt.bean.entities.interfaces.ClassRoomInterface;
import de.revdev.llt.bean.facade.CategoryFacade;
import de.revdev.llt.bean.facade.FileCardBoxFacade;
import de.revdev.llt.bean.facade.PrivateClassRoomFacade;
import de.revdev.llt.bean.facade.PublicClassRoomFacade;
import de.revdev.llt.bean.facade.UserFacade;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "fileCardBoxBean")
@RequestScoped
public class FileCardBoxBean implements Serializable {

    private UserBean userBean;
    private ShowClassRoomBean showClassRoomBean;
    private UserFacade uf;
    private FileCardBoxFacade fcbf;
    private PublicClassRoomFacade pucrf;
    private PrivateClassRoomFacade prcrf;
    private CategoryFacade cf;
    private static final Logger log = Logger.getLogger(FileCardBoxBean.class.getName());
    private FileCardBox fcb;
    private Category cat1;
    private Category cat2;
    private Category cat3;
    private ClassRoomInterface classRoom;

    /**
     * Creates a new instance of FileCardBoxBean
     */
    public FileCardBoxBean() {
    }

    @PostConstruct
    private void init() {
        fcb = new FileCardBox();
    }

//    public void loadClassRoom(ActionEvent event) {
//        String type = (String) event.getComponent().getAttributes().get("cltype");
//        long id = (long) event.getComponent().getAttributes().get("clid");
//        if (type.equals("public")) {
//            classRoom = pucrf.find(id);
//        } else {
//            classRoom = prcrf.find(id);
//        }
//    }
    public void loadClassRoom(ActionEvent event) {
        classRoom = showClassRoomBean.getClassRoom();
    }

    public void save() {
        classRoom = showClassRoomBean.getClassRoom();
        if (fcb.getPublicClassRooms() == null) {
            fcb.setPublicClassRooms(new LinkedList<PublicClassRoom>());
        }
        fcb.setCategory(checkCategory());
        fcbf.create(fcb);
        if (classRoom instanceof PublicClassRoom) {
            PublicClassRoom pcr = (PublicClassRoom) classRoom;
            classRoom.getFileCardBoxes().add(fcb);
            fcb.getPublicClassRooms().add(pcr);
            fcbf.edit(fcb);
            pucrf.edit((PublicClassRoom) classRoom);
        } else {
            fcb.getPrivateClassRooms().add(userBean.getUser().getPrivateClassRoom());
            userBean.getUser().getPrivateClassRoom().getFileCardBoxes().add(fcb);
            fcbf.edit(fcb);
            prcrf.edit(userBean.getUser().getPrivateClassRoom());
        }
        userBean.refreshUser();
        showClassRoomBean.refreshClassRoom();
        log.log(Level.INFO, "FCB {0} was created and attached to PrCR {1}", new Object[]{fcb.getId(), classRoom.getId()});
        cancel();
    }

    public Category checkCategory() {
        Category tmp = cat1;
        if (cat2 != null) {
            tmp = cat2;
        } else if (cat3 != null) {
            tmp = cat3;
        }
        return tmp;
    }

    public void delete(ActionEvent event) {
        long fcbid = (long) event.getComponent().getAttributes().get("fcbid");
        classRoom = showClassRoomBean.getClassRoom();
        FileCardBox fileCardBox = fcbf.find(fcbid);
        if (classRoom instanceof PublicClassRoom) {
            PublicClassRoom pcr = (PublicClassRoom) classRoom;
            pcr.getFileCardBoxes().remove(fileCardBox);
            fileCardBox.getPublicClassRooms().remove(pcr);
            pucrf.edit((PublicClassRoom) showClassRoomBean.getClassRoom());
            fcbf.remove(fileCardBox);
        } else {
            userBean.getUser().getPrivateClassRoom().getFileCardBoxes().remove(fileCardBox);
            uf.edit(userBean.getUser());
        }
        showClassRoomBean.refreshClassRoom();
        log.log(Level.INFO, "FCB {0} was detached from PCR {1}", new Object[]{fileCardBox.getId(), classRoom.getId()});
        cancel();
    }

    public void cancel() {
        fcb = new FileCardBox();
        cat1 = null;
        cat2 = null;
        cat3 = null;
    }

    public List<Category> getAllCategories() {
        return cf.findAllRootCategories();
    }

    @Inject
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    @Inject
    public void setCategoryFacade(CategoryFacade categoryFacade) {
        this.cf = categoryFacade;
    }

    @Inject
    public void setUserFacade(UserFacade userFacade) {
        this.uf = userFacade;
    }

    @Inject
    public void setFileCardBoxFacade(FileCardBoxFacade fileCardBoxFacade) {
        this.fcbf = fileCardBoxFacade;
    }

    @Inject
    public void setPrivateClassRoomFacade(PrivateClassRoomFacade privateClassRoomFacade) {
        this.prcrf = privateClassRoomFacade;
    }

    @Inject
    public void setPublicClassRoomFacade(PublicClassRoomFacade publicClassRoomFacade) {
        this.pucrf = publicClassRoomFacade;
    }

    @Inject
    public void setShowClassRoomBean(ShowClassRoomBean showClassRoomBean) {
        this.showClassRoomBean = showClassRoomBean;
    }

    public FileCardBox getFcb() {
        return fcb;
    }

    public void setFcb(FileCardBox fcb) {
        this.fcb = fcb;
    }

    public Category getCat1() {
        return cat1;
    }

    public void setCat1(Category cat1) {
        this.cat1 = cat1;
    }

    public Category getCat2() {
        return cat2;
    }

    public void setCat2(Category cat2) {
        this.cat2 = cat2;
    }

    public Category getCat3() {
        return cat3;
    }

    public void setCat3(Category cat3) {
        this.cat3 = cat3;
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public ClassRoomInterface getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(ClassRoomInterface classRoom) {
        this.classRoom = classRoom;
    }
}
